package com.bidevalution.support.util;

import com.bidevalution.support.context.Constants;
import com.healthmarketscience.jackcess.*;
import lombok.extern.log4j.Log4j2;
import net.ucanaccess.jdbc.JackcessOpenerInterface;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.*;

import org.apache.log4j.Logger;


/**
 * java读取access文件工具类
 * Created by glodon on 2019-08-02.
 */
@Log4j2
public class MdbUtils implements JackcessOpenerInterface {
    private static Database db = null;
    private static Logger log = Logger.getLogger(JackcessOpenerInterface.class);

    /**
     * 读取mdb文件，不需要密码
     *
     * @param file mdb文件
     * @return
     * @throws IOException
     */
    public Database open(File file) throws IOException {
        DatabaseBuilder dbd = new DatabaseBuilder(file);
        dbd.setAutoSync(false);
        dbd.setCodecProvider(new CryptCodecProvider(Constants.EMPTY_STRING));
        dbd.setReadOnly(false);
        return dbd.open();
    }

    /**
     * 读取mdb文件，需要密码
     *
     * @param file mdb文件
     * @param pwd  密码
     * @return
     * @throws IOException
     */
    @Override
    public Database open(File file, String pwd) throws IOException {
        DatabaseBuilder dbd = new DatabaseBuilder(file);
        dbd.setAutoSync(false);
        dbd.setCodecProvider(new CryptCodecProvider(pwd));
        dbd.setReadOnly(false);
        return dbd.open();
    }

    /**
     * 根据文件名获取表数据
     * @param filename
     * @return
     */
    public static List<Table> getTableListFromMdb(String filename) {
        List<Table> tables= new ArrayList<>();
        File file = new File(filename);
        MdbUtils rm = new MdbUtils();
        Table table = null;
        try {
            db = rm.open(file);
            Set<String> tableNames = db.getTableNames();
            for (String tableName : tableNames) {
                table = db.getTable(tableName);
                tables.add(table);
            }
        } catch (IOException e1) {
            log.error("读取mdb格式文件失败", e1);
        }

        return tables;
    }

    /**
     * 关闭mdb文件
     *
     * @return
     * @throws IOException
     */
    public static void close() throws IOException {
        if(db != null){
            db.close();
        }
    }

    public static void main(String[] args) throws SQLException {
        List<Table> tables = getTableListFromMdb("E:\\工作\\文档\\开评标管理系统\\评标管理系统\\杭州投标文件\\(1234567890987654321)擎洲测试杭州一分公司.网络加密标书.加密标书.temp.TB - 副本.mdb");
        for (Table table: tables) {
            //if(table.getRowCount() > 0) {
                if(("td_Appendix").equals(table.getName())) {
                    System.out.println(table.getPrimaryKeyIndex().getColumns().get(0).getName());

                    //System.out.println(table.getName()+"------------------------------------------------------------------------------ ：" + table.getRowCount());
                    List<? extends Column> columnList = table.getColumns();
                //    for (Row row : table) {
                        for (Column column : columnList) {
                            System.out.println(column.getLengthInUnits());
                            //System.out.println("columnValue++++++++++++++++++++++++++++++++" + row.get(column.getName()));
                            System.out.println(column.getType().name());
                        }
               //     }
                }
          //  }
        }
    }
}